package com.hotel.front.controller.company;

import com.hotel.comm.bean.DubboReply;
import com.hotel.comm.bean.IReply;
import com.hotel.comm.bean.company.EjobCompanyUser;
import com.hotel.comm.service.company.ICompanyUserService;
import com.hotel.front.bean.Constants;
import com.hotel.front.bean.FieldValidatorResponse;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by frank on 15-6-18.
 */
@Controller
public class CompanyUserController {

    @Autowired
    ICompanyUserService companyUserService;

    private static final Logger LOGGER = LoggerFactory.getLogger(CompanyUserController.class);

    /**
     * 创建企业用户
     *
     * @param ejobCompanyUser
     * @param randomCode
     * @param request
     * @return
     */
    @RequestMapping(value = "/create_company_user", method = RequestMethod.POST)
    public String createCompanyUser(EjobCompanyUser ejobCompanyUser, @RequestParam(value = "randomCode", defaultValue = "") String randomCode,
                                    HttpServletRequest request) {
        LOGGER.info("loginName:" + ejobCompanyUser.getLoginName() + ",password:" + ejobCompanyUser.getPassword() + "randomCode:" + randomCode);
        if (StringUtils.isNotBlank(ejobCompanyUser.getLoginName()) && StringUtils.isNotBlank(ejobCompanyUser.getPassword()) && StringUtils.isNotBlank(ejobCompanyUser.getEmail())) {
            String randomCodeStore = (String) request.getSession().getAttribute(Constants.RANDOM_IN_SESSION);

            //验证码验证
            if (StringUtils.equals(randomCode, randomCodeStore)) {
                DubboReply reply = (DubboReply) companyUserService.save(ejobCompanyUser);
                if (reply != null && reply.isSuccess()) {
                    request.getSession().setAttribute(Constants.LOGIN_USER, ejobCompanyUser);
                } else {
                    LOGGER.info(reply.getReplyMsg());
                }
            }
        }
        return Constants.INDEX;
    }

    /**
     * 企业用户注册时检查用户名是否存在
     *
     * @param loginName
     * @return
     */
    @RequestMapping(value = "/checkCompanyUserExist")
    @ResponseBody
    public FieldValidatorResponse checkRegisterCompanyUserExist(@RequestParam(value = "loginName") String loginName) {
        FieldValidatorResponse remoteFieldValidatorResponse = new FieldValidatorResponse();
        remoteFieldValidatorResponse.setValid(true);
        if (StringUtils.isNotBlank(loginName)) {
            IReply reply = companyUserService.findByLoginName(loginName);
            if (reply != null && reply.isSuccess()) {
                EjobCompanyUser companyUser = (EjobCompanyUser) ((DubboReply) reply).getT();
                if (companyUser != null) {
                    remoteFieldValidatorResponse.setValid(false);
                }
            }
        }
        return remoteFieldValidatorResponse;
    }

    /**
     * 企业用户登录
     *
     * @param ejobCompanyUser
     * @param request
     * @return
     */
    @RequestMapping(value = "/login_company_user", method = RequestMethod.POST)
    public String loginCompanyUser(EjobCompanyUser ejobCompanyUser, HttpServletRequest request) {
        LOGGER.info("loginName:" + ejobCompanyUser.getLoginName() + ",password:" + ejobCompanyUser.getPassword());
        IReply reply = companyUserService.findByLoginName(ejobCompanyUser.getLoginName());
        if (reply != null && reply.isSuccess()) {
            EjobCompanyUser existCompanyUser = (EjobCompanyUser) ((DubboReply) reply).getT();
            if (existCompanyUser != null && StringUtils.equals(existCompanyUser.getPassword(), DigestUtils.md5Hex(ejobCompanyUser.getPassword()))) {
                request.getSession().setAttribute(Constants.LOGIN_USER, existCompanyUser);
            }
        }
        return Constants.INDEX;
    }

    /**
     * 企业用户登录时检查用户名是否存在
     *
     * @param loginName
     * @return
     */
    @RequestMapping(value = "/checkLoginCompanyUserExist")
    @ResponseBody
    public FieldValidatorResponse checkLoginCompanyUserExist(@RequestParam(value = "loginName") String loginName) {
        FieldValidatorResponse remoteFieldValidatorResponse = new FieldValidatorResponse();
        remoteFieldValidatorResponse.setValid(false);
        if (StringUtils.isNotBlank(loginName)) {
            IReply reply = companyUserService.findByLoginName(loginName);
            if (reply != null && reply.isSuccess()) {
                EjobCompanyUser companyUser = (EjobCompanyUser) ((DubboReply) reply).getT();
                if (companyUser != null) {
                    remoteFieldValidatorResponse.setValid(true);
                }
            }
        }
        return remoteFieldValidatorResponse;
    }

    /**
     * 企业用户登录时检查密码是否正确
     *
     * @param ejobCompanyUser
     * @return
     */
    @RequestMapping(value = "/checkLoginCompanyUserPasswordValid")
    @ResponseBody
    public FieldValidatorResponse checkLoginCompanyUserPasswordValid(EjobCompanyUser ejobCompanyUser) {
        FieldValidatorResponse remoteFieldValidatorResponse = new FieldValidatorResponse();
        remoteFieldValidatorResponse.setValid(false);
        if (StringUtils.isNotBlank(ejobCompanyUser.getLoginName())) {
            IReply reply = companyUserService.findByLoginName(ejobCompanyUser.getLoginName());
            if (reply != null && reply.isSuccess()) {
                EjobCompanyUser existCompanyUser = (EjobCompanyUser) ((DubboReply) reply).getT();
                if (existCompanyUser != null && StringUtils.equals(existCompanyUser.getPassword(), DigestUtils.md5Hex(ejobCompanyUser.getPassword()))) {
                    remoteFieldValidatorResponse.setValid(true);
                }
            }
        }
        return remoteFieldValidatorResponse;
    }

    @RequestMapping(value = "/update_company_user", method = RequestMethod.POST)
    @ResponseBody
    public DubboReply updateCompanyUser(EjobCompanyUser ejobCompanyUser) {
        if (ejobCompanyUser == null || ejobCompanyUser.getId() == null) {
            return new DubboReply(Constants.PARAMETER_ERROR_CODE, Constants.PARAMETER_ERROR_MSG);
        }
        return (DubboReply) companyUserService.update(ejobCompanyUser);
    }

    @RequestMapping(value = "/delete_company_user")
    @ResponseBody
    public DubboReply deleteCompanyUser(@RequestParam(value = "id", defaultValue = "") Long id) {
        if (id == null) {
            return new DubboReply(Constants.PARAMETER_ERROR_CODE, Constants.PARAMETER_ERROR_MSG);
        }
        return (DubboReply) companyUserService.delete(id);
    }

    @RequestMapping(value = "/get_company_user_by_id")
    @ResponseBody
    public EjobCompanyUser getCompanyUserById(@RequestParam(value = "id", defaultValue = "") Long id) {
        if (id == null) return null;
        DubboReply reply = (DubboReply) companyUserService.getById(id);
        if (reply != null && reply.isSuccess()) {
            EjobCompanyUser companyUser = (EjobCompanyUser) ((DubboReply) reply).getT();
            if (companyUser != null) return companyUser;
        } else {
            LOGGER.info(reply.getReplyMsg());
        }
        return null;
    }
}
